﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using XXX.Models.XXXEntities;
using XXX.Bo;
using XXX.Models;
using LH.Model.PU;
using NETCore.MailKit.Core;
using Newtonsoft.Json.Linq;
using XXX.Common;
using fuzhu;
using LH.Model.ST;
using Yonyou.U8.BO;
using XXX.api.Filter;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace XXX.api.Controllers
{


    /// <summary>
    ///  其他出库单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [TypeFilter(typeof(ApiLoggingFilterFactory))]
    public class ST_Rd09Controller : ControllerBase
    {
        private readonly ILogger<ST_Rd09Controller> _logger;
        //private readonly IEmailService _EmailService;

        public ST_Rd09Controller(ILogger<ST_Rd09Controller> logger )
        {
            _logger = logger; 
        }


        [HttpPost]
        public RetModel2 AddVerify(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);
            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }
            #region 接口调用间隔
            string strKey = ControllerContext.RouteData.Values["controller"].ToString() + Md5Encrypt.MD5(requestBody);

            if (CacheHelpCore.GetCacheValue(strKey) == null)
            {
                CacheHelpCore.SetChacheValue(strKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CanShu.chkRepeat);
            }
            else
            {
                throw new Exception("接口调用间隔过短");
            }
            #endregion 接口调用间隔
            try
            {
                string cErr = "";
                string cCode = "";
                API_Rdrecord api_entity;
                try
                {
                    api_entity = JsonConvert.DeserializeObject<API_Rdrecord>(requestBody);
                    if (api_entity == null)
                    {
                        cErr = "json结构异常!空值";
                        return null;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("json结构异常！" + e.Message);
                }
                if (string.IsNullOrWhiteSpace(api_entity.cCode))
                {
                    cErr = "其他入库单号不能为空";
                    return null;
                }
                RdRecord09VM model = new RdRecord09VM(new ST_Rdrecord09.Head(), new List<ST_Rdrecord09.Body>());

                model = model.GetAddEntity(api_entity, ref cErr);
                RetModel2 ret;
                if (model == null)
                {
                    ret = DBService.ReErrObject("入参有误" + cErr);
                }
                else
                {


                    ret = model.AddVerify();
                    //model.conStr = CanShu.conStr;                   

                }
                 
                return ret;
            }
            catch (Exception ex)
            {
                //_EmailService.Send(CanShu.mail.ToAddress, CanShu.cZT + "产成品入库单新增,程序错误：", "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ex.Message);
                return DBService.ReErrObject(ex.Message);
            }




        }

        [HttpPost]
        public RetModel2 UNVerify(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);
            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }

            try
            {
                string cErr = "";
                API_Rdrecord api_entity;
                try
                {
                    api_entity = JsonConvert.DeserializeObject<API_Rdrecord>(requestBody);
                    if (api_entity == null)
                    {
                        cErr = "json结构异常!空值";
                        return null;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("json结构异常！" + e.Message);
                }
                RdRecord09VM model = new RdRecord09VM(new ST_Rdrecord09.Head(), new List<ST_Rdrecord09.Body>());

                model = model.GetCodeEntity(api_entity, ref cErr);
                RetModel2 ret;
                if (model == null)
                {
                    ret = DBService.ReErrObject("入参有误" + cErr);
                }
                else
                {
                    ret = model.UnverifyDelete();
                }
                // 将 JSON 数据保存到数据库中
                JsonSaveService.saveLog2("其他入库单弃审删除", model.head.ccode, requestBody, ret.code, ret.message);


                return ret;
            }
            catch (Exception ex)
            {
                //_EmailService.Send(CanShu.mail.ToAddress, CanShu.cZT + "销售订单弃审删除,程序错误：", "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ex.Message);
                return DBService.ReErrObject(ex.Message);
            }

        }


    }
















}
